package com.freek.ScreenDrop;

import java.awt.Desktop;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.prefs.Preferences;

import javax.swing.JOptionPane;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.exception.DropboxUnlinkedException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.RequestTokenPair;
import com.dropbox.client2.session.Session.AccessType;
import com.dropbox.client2.session.WebAuthSession;
import com.dropbox.client2.session.WebAuthSession.WebAuthInfo;

public class Authenticator
{
	public static DropboxAPI<WebAuthSession> dropboxAPI = null;
	private static final String PREF_TOKEN_KEY = "PREF_TOKEN_KEY";
	private static final String PREF_TOKEN_SECRET = "PREF_TOKEN_SECRET";
	
	private static final String APP_KEY = "tsae50pn4nzl6vw";
	private static final String APP_SECRET = "r5u44o6ch2whlrs";
	private static final AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
	
	public static DropboxAPI<WebAuthSession> authenticate() throws DropboxException, MalformedURLException, IOException, URISyntaxException
	{
		Preferences prefs = Preferences.userNodeForPackage(Authenticator.class);
		String tokenKey = prefs.get(PREF_TOKEN_KEY, null);
		String tokenSecret = prefs.get(PREF_TOKEN_SECRET, null);
		AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
		WebAuthSession session;
		if(tokenKey != null && tokenSecret !=null)
		{
			session = new WebAuthSession(appKeys, ACCESS_TYPE, new AccessTokenPair(tokenKey, tokenSecret));
			if(!session.isLinked())
			{
				link(session);
			}
		}
		else
		{
			session = new WebAuthSession(appKeys, ACCESS_TYPE);	
			link(session);
		}
		dropboxAPI = new DropboxAPI<WebAuthSession>(session);
		try
		{
			//trivial request to check if linked
			dropboxAPI.accountInfo();
		} catch (DropboxUnlinkedException e) {
			session = new WebAuthSession(appKeys, ACCESS_TYPE);	
			link(session);
			dropboxAPI = new DropboxAPI<WebAuthSession>(session);
		}
		return new DropboxAPI<WebAuthSession>(session);
	}
	
	
	public static void link(WebAuthSession session) throws DropboxException, MalformedURLException, IOException, URISyntaxException
	{
		try
		{
			Preferences prefs = Preferences.userNodeForPackage(Authenticator.class);
	
			WebAuthInfo authInfo = session.getAuthInfo();
			
			RequestTokenPair pair = authInfo.requestTokenPair;
			String url = authInfo.url;
			
			Desktop.getDesktop().browse(new URL(url).toURI());
			JOptionPane.showMessageDialog(null, "Press okay to continue once you have finished authenticating");
			session.retrieveWebAccessToken(pair);
			AccessTokenPair tokens = session.getAccessTokenPair();
			prefs.put(PREF_TOKEN_KEY, tokens.key);
			prefs.put(PREF_TOKEN_SECRET, tokens.secret);
		} catch (DropboxUnlinkedException e) {
			int response = JOptionPane.showConfirmDialog(null, "ScreenDrop was unable to link to your Dropbox account. Attempt to link again?","Unable to link to Dropbox",JOptionPane.YES_NO_OPTION);
			if(response == JOptionPane.YES_OPTION)
			{
				AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
				session = new WebAuthSession(appKeys, ACCESS_TYPE);	
				link(session);
			}
		}
	}
}
